/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package harjoitukset;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JApplet;
import javax.swing.JPanel;

/**
 *
 * @author f0899
 */
public class Tehtava2 extends JApplet {

    private int alkuX, alkuY, loppuX, loppuY;

    @Override
    public void init() {
        // kuuntelija hiirelle
        addMouseMotionListener(new HiirenTarkkailija());
        addMouseListener(new HiirtaPainettu());
        getContentPane().add(new PiirtoPinta());
    }

    class HiirenTarkkailija extends MouseMotionAdapter {

        @Override
        public void mouseDragged(MouseEvent event) {
            loppuX = event.getX();
            loppuY = event.getY();
            repaint();
        }
    }

    class HiirtaPainettu extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            alkuX = e.getX();
            alkuY = e.getY();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            loppuX = e.getX();
            loppuY = e.getY();
            repaint();
        }
    }

    public class PiirtoPinta extends JPanel {

        @Override
        public void paintComponent(Graphics g) {
            g.drawRect(alkuX,alkuY,loppuX-alkuX,loppuY-alkuY);
        }
    }
}
